<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\AdminUser;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;

require_once 'resources/org/captcha/Code.class.php';

class LoginController extends Controller {
    public function login() {
        if ($input = Input::all()){
            /*$code = new \Code();
            $captcha = $code->get();
            if (strtolower($captcha) != strtolower($input['captcha'])){
                return back()->with('msg', '验证码不正确');
            }*/
            $user = AdminUser::where(['username'=>$input['username']])->first();
            if ($input['password'] != Crypt::decrypt($user->password)){
                return back()->with('msg','用户名或密码不正确！');
            }else{
                session(['user'=>$user]);
                return redirect('admin/index')->with('msg','登录成功');
            }
        }
        return view('admin.login');
    }

    public function index(){
        return view('admin.index');
    }

    public function captcha() {
        $captcha = new \Code();
        $captcha->width(140);
        $captcha->height(38);
        $captcha->make();
    }

    public function logout() {
        echo 'logout';
    }

    public function crypt(){
        echo Crypt::encrypt('123456');
    }

}
